#include <54func.h>

//定义学生结构体
typedef struct student_s
{
    int num;
    char name[30];
    float score;
}student_t;
int main(int argc,char *argv[])
{
    // ./0_homework_student file //要写文件
    ARGS_CHECK(argc,2);
    int fd = open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666);
    ERROR_CHECK(fd,-1,"open");

    // 给数组赋值
    student_t stu[3]={
        {1,"Caixvkun",88.8},
        {2,"Wuyifan",66.6},
        {3,"Liyifeng",55.5},
    };

    //写入文件
    write(fd,stu,sizeof(stu));

    // 通过lseek,将pos从当前（末尾）偏移到开头
    lseek(fd,0,SEEK_CUR);

    //读文件
    //创建读取文件数组
    student_t arr[3];
    read(fd,arr,sizeof(stu));

    for(int i=0;i<3;i++)
    {
        printf("num = %d,name = %s,score = %f\n",stu[i].num,stu[i].name,stu[i].score);
    }


    return 0;
}
